#ifndef TCP_CLIENT_H
#define TCP_CLIENT_H

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <vector>
#include <mutex>
#include <QVariant>


#include "CollectBase.h"
#include "BRE_EEG_dataStruct.h"


#define MAXLINE 4096

typedef struct getDataStr
{
    // 是否将数据写入文件
    int writeInFile;
    // 客户端文件描述符
    int clientSockFd;
    // 数据缓冲区
    char buffer[MAXLINE];

    // 解包后的数据放在这里
    std::vector <EEG_VOLTAGE> m_aryVoltages;
}getDataStr_TypeDef;


class EEG_device : public CollectBase
{
    Q_OBJECT

public:
    // 获取单实例对象
    static EEG_device *getInstance(QObject *parent=nullptr);
    // static ConfigImport *getInstance(std::string yaml_loadFile = "../yaml/loadFile.yaml");
    //释放单实例，进程退出时调用
    static void deleteInstance();

private:

    explicit EEG_device(QObject *parent=nullptr, const char* serverIP="192.168.1.180",int serverPort = 5001);
    ~EEG_device();

    // 将其拷贝构造和赋值构造成为私有函数, 禁止外部拷贝和赋值
    EEG_device(const EEG_device &);
    const EEG_device &operator=(const EEG_device &);
    // 唯一单实例对象指针
    static EEG_device *m_SingleInstance;

public:

    int dataCollecting(void* args = nullptr) override;
    const QMap<int,QString>& getElectrodeMap() override;

    // void collectingEEG(bool state);
    // bool is_collecting();
    // void stopCollecting();



    getDataStr_TypeDef* get_ClientData();

protected:
    void run() override;


signals:
    // void send_ClientData(getDataStr_TypeDef* data);
    // void send_ClientData(void* data);
    // void send_ClientData(std::shared_ptr<QVector<EEG_VOLTAGE>> eegData_shared_ptr);
    // void send_ClientData(const std::vector <EEG_VOLTAGE>& eegData);
    void send_ClientData(const QVariant& eegData);
    // void send_ClientData(const std::vector <EEG_VOLTAGE>& data);
    
    // void colllectingStop();

// public slots:
//     void collectingEEG(bool state);

private:
    /* data */
    // 连接成功标志
    // bool is_connected;
    // 是否开始采集(获取服务器数据)
    // static bool is_collecting;

    static std::mutex m_Mutex;
    struct sockaddr_in  serverSock;

    // int clientSockFd;
    // 向服务端申请数据事件，数据准备好后，该事件为真
    // Event dataPreparedEvent;
    // char  buffer[MAXLINE];

    // 将上述成员放进结构，方便传参
    getDataStr_TypeDef ClientData;

    std::vector <EEG_VOLTAGE> sendDataInsignals;
    // QMap<int, QString> electrodeMap;


};




#endif // !TCP_CLIENT_H

